clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 015_analysis_dropUrban, replace


*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	015_analysis_dropUrban.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/20/2021

Purpose:		

Data Used:      votes_wind_panel_urban.dta
				election_district_panel_urban.dta
				

Program Used:   - reghdfe -

*/

*******************************************************************************
/*                   			   Table A9								     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use election_district_panel_urban.dta, clear

** Create instrument and fixed effects
gen t = year - 2004
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen district_fixed = group(state district)

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )

gen turnout = votes1 + votes2 + votes3 + votes4
gen lnturnout = log(turnout)


local outcome demvotesmajorpercent repvotesmajorpercent incumbvotesmajorpercent  // outcome variable 
local endogenous cum_capacity_turbine cum_count_turbine  // endogenous variable
local instrument inter // instrument
local admin1_trend stateyear_fixed // geography * time trend
local admin2 district_fixed // panel unit (cluster variable)


foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	local y_name = substr("`y'", 1, 3)
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument') if frac_urbanArea < 0.5, absorb(`admin1_trend' `admin2') ffirst stages(first ols reduced) vce(cluster `admin2') old
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
			estimates restore reghdfe_first1
			estimates store `y_name'_`x_name'_first
			estimates restore reghdfe_ols
			estimates store `y_name'_`x_name'_ols
			estimates restore reghdfe_reduced
			estimates store `y_name'_`x_name'_reduced
		}
}

*--------------------- Export LaTeX regression tables -----------------------*
cd "$rootDir/$resultDir/Tables"

** IV estimates only
esttab dem_capacity_iv dem_count_iv rep_capacity_iv rep_count_iv ///
		inc_capacity_iv inc_count_iv using TableA9.tex, booktabs replace ///
		refcat(cum_capacity_turbine "\emph{Panel: IV}", nolabel) ///
		b(%9.3f) se noconstant nonotes legend star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(cum_capacity_turbine "Cumulative capacity (MW)" cum_count_turbine "Cumulative count") varwidth(27) modelwidth(13) ///
		stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Democratic Vote" "Republican Vote" "Incumbent Vote", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)


*******************************************************************************
/*                   			   Table A10								 */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel_urban.dta, clear

** Create instrument and fixed effects
gen t = year - 2003
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen district_fixed = group(state district)


local outcome pro_env anti_env // outcome variable 
local endogenous cum_capacity_turbine cum_count_turbine // endogenous variable
local instrument inter // instrument
local admin1_trend stateyear_fixed // geography * time trend
local admin2 district_fixed // panel unit (cluster variable)

foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	tokenize "`y'", parse("_")
	local y_name "`1'"
	di "`y_name'"
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument') if frac_urbanArea < 0.5, absorb(`admin1_trend' `admin2') ffirst stages(first ols reduced) vce(cluster `admin2') old
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
			estimates restore reghdfe_first1
			estimates store `y_name'_`x_name'_first
			estimates restore reghdfe_ols
			estimates store `y_name'_`x_name'_ols
			estimates restore reghdfe_reduced
			estimates store `y_name'_`x_name'_reduced
		}
}


*--------------------- Export LaTeX regression tables -----------------------*
cd "$rootDir/$resultDir/Tables"

** IV estimates only
esttab pro_capacity_iv pro_count_iv anti_capacity_iv anti_count_iv using TableA10.tex, booktabs replace ///
		se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_capacity_turbine "Cumulative capacity (MW)" cum_count_turbine "Cumulative count") varwidth(27) modelwidth(13) ///
		mtitles("Model" "Model" "Model" "Model") ///
		mgroups("Pro-Environment Vote" "Anti-Environment Vote", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		refcat(cum_capacity_turbine "\emph{Panel: IV}", nolabel) ///
		width(\hsize)

		
** Close log file
log close
cd "$rootDir/$logDir"
translate 015_analysis_dropUrban.smcl 015_analysis_dropUrban.log, replace

